/*
 * Created on 2005-7-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.vsg.common.pagination.web.taglibs;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.wrapper.PaginationWrapper;
import org.vsg.common.pagination.wrapper.WebPaginationWrapper;

/**
 * @author bill vison
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PaginationBarTag extends TagSupport {
	
	final static Logger logger = LoggerFactory.getLogger(PaginationBarTag.class);
	
	// reset the page url
	protected String url;
	
	// set the paginationbar tag
	protected Pagination pagination;
	
	/**
	 * the WebPaginationBean id saved in the request attribute
	 */
	protected String name;
	 
	
	
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param url The url to set.
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	
	public Pagination getPagination() {
		return this.pagination;
	}
	
	
	// ---------------------------------------- protectd methods
	protected void writeFormTag(String action) {
		StringBuffer formTag = new StringBuffer();
		formTag.append("<form name=\"").append(id).append("\" ");
		formTag.append("id=\"").append(id).append("\" ");
		formTag.append("method=\"").append("get").append("\" ");
		formTag.append("action=\"").append(action).append("\" ");
		formTag.append("onsubmit=\"return goToPage(this)\" ");
		formTag.append("/>");
		JspWriter writer = pageContext.getOut();
		try {
			writer.write(formTag.toString());			
		} catch (IOException ioe) {
			ioe.printStackTrace();			
		}		
	}
	
	/**
	 * write javascript for the go ot page field
	 * @throws JspException
	 */
	protected void writeGoToPageJs(String goToPageParamName) throws JspException{
		StringBuffer jsTag = new StringBuffer();
		
		// start tag
		jsTag.append("\n<script language=\"JavaScript\" type=\"text/javascript\">").append("\n");
		
		/** javascript function goToPage() **/
		jsTag.append("function goToPage(formObj) {").append("\n");
		jsTag.append("\t").append("var actionUrl = formObj.action;").append("\n");
		jsTag.append("\t").append("var pageIndexValue = formObj.pageIndex.value;").append("\n");
		jsTag.append("\t")
				.append("formObj.action = actionUrl + \"&")
				.append(goToPageParamName)
				.append("=\" + ")
				.append("pageIndexValue;")
				.append("\n");
		jsTag.append("\t").append("var passValidate = true;").append("\n");
		jsTag.append("\t").append("var vali = new PaginationValidate();").append("\n");
		jsTag.append("\t").append("if (!vali.integerCheck(formObj.pageIndex,\"ֻ���������ͣ�\")) {").append("\n");
		jsTag.append("\t\t").append("passValidate = false;").append("\n");
		jsTag.append("\t").append("}").append("\n");

		jsTag.append("\t").append("if (passValidate) {").append("\n");
		jsTag.append("\t\t").append("window.location = formObj.action;").append("\n");
		jsTag.append("\t").append("}").append("\n");		
		jsTag.append("\t").append("return false;").append("\n");
		jsTag.append("} ").append("\n");
		
		/** javascript function PaginationValidate() **/
		jsTag.append("function PaginationValidate() {").append("\n");
		
		jsTag.append("\t").append("function showMessage(obj,msg) {").append("\n");
		jsTag.append("\t\t").append("alert(msg);").append("\n");
		jsTag.append("\t\t").append("obj.focus();").append("\n");
		jsTag.append("\t").append("}").append("\n");
		
		/** javascript function integerCheck(obj,msg) //��� ֻ����������ֵ **/
		jsTag.append("\t").append("function integerCheck(obj,msg) {").append("\n");
		jsTag.append("\t\t").append("var reg = /^([0-9])+$/;").append("\n");
		jsTag.append("\t\t").append("return this.validCheck(reg,obj,msg);").append("\n");
		jsTag.append("\t").append("}").append("\n");


		jsTag.append("\t").append("function validCheck(reg,obj,msg) {").append("\n");
		jsTag.append("\t\t").append("if(!msg) {").append("\n");
		jsTag.append("\t\t\t").append("return obj.match(reg);").append("\n");
		jsTag.append("\t\t").append("}").append("\n");	
		jsTag.append("\t\t").append("var r = validCheck(reg,obj.value);").append("\n");	
		jsTag.append("\t\t").append("if(!r) {").append("\n");	
		jsTag.append("\t\t\t").append("this.showMessage(obj,msg);").append("\n");
		jsTag.append("\t\t").append("}").append("\n");	
		jsTag.append("\t\t").append("return r;").append("\n");	
		jsTag.append("\t").append("}").append("\n");

		jsTag.append("\t").append("this.showMessage = showMessage;").append("\n");	
		jsTag.append("\t").append("this.validCheck = validCheck;").append("\n");	
		jsTag.append("\t").append("this.integerCheck = integerCheck;").append("\n");	
		
		jsTag.append("}").append("\n");			
		// close tag
		jsTag.append("</script>").append("\n");
		
		JspWriter writer = pageContext.getOut();
		try {
			writer.write(jsTag.toString());			
		} catch (IOException ioe) {
			ioe.printStackTrace();			
		}			
	}
	
	protected void writeCloseFormTag() {
		StringBuffer formTag = new StringBuffer();
		formTag.append("</form>");
		JspWriter writer = pageContext.getOut();
		try {
			writer.write(formTag.toString());			
		} catch (IOException ioe) {
			ioe.printStackTrace();			
		}		
	}
	

	
	public int doStartTag() throws JspException {
		
		Object objref = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
		if (objref == null) {
			objref = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
		}
		if (objref == null) {
			objref = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
		}
		if (objref == null) {
			objref = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
		}
		
		PaginationWrapper pb = null;
		if (objref instanceof PaginationWrapper) {
			pb = (PaginationWrapper)objref;
		
			if (this.url != null && !this.url.equals("")) {
				WebPaginationWrapper wpb = (WebPaginationWrapper)pb;
				wpb.setUrl(this.url);
			}
			
			pagination = pb.getPagination();
			if (logger.isDebugEnabled()) {
				logger.debug("Load in Web Pagination Bean [" + pagination+ "] ");
			}
			// set the pagination bar for data
			pageContext.setAttribute(id, pb);
		}
		
		// set the variable attribute for data
		if (pageContext.getAttribute("currentPageNumber") == null) {
			pageContext.setAttribute("currentPageNumber",new Integer(pb.getCurrentPage()));
		}
		
		if (pageContext.getAttribute("currentPageUrl") == null) {
			if (pb instanceof WebPaginationWrapper) {
				String pageIndexUrl = ((WebPaginationWrapper)pb).getNumPageUrl(pb.getCurrentPage());
				pageContext.setAttribute("currentPageUrl",pageIndexUrl);
			}
		}
		
		return EVAL_BODY_INCLUDE;
	}
	
	public int doEndTag() throws  JspException {
		return this.EVAL_PAGE;
	}
	

}
